import { get, post, patch, del } from '@/util/api';
import { commentApi } from '@/constants';

export interface SensitiveWord {
  id: number;
  word: string;
  usageCount: number;
  isActive: number;
  createdAt: string;
  updatedAt: string;
}

export interface SensitiveWordCreateDto {
  word: string;
}

export interface SensitiveWordBatchCreateDto {
  words: string[];
}

class SensitiveWordService {
  /**
   * 获取所有敏感词
   */
  async getAllSensitiveWords(): Promise<SensitiveWord[]> {
    try {
      const response = await get(commentApi.SENSITIVE_WORDS);
      return (response.data || response) as SensitiveWord[];
    } catch (error) {
      console.error('获取敏感词列表失败:', error);
      throw error;
    }
  }

  /**
   * 创建敏感词
   */
  async createSensitiveWord(word: string): Promise<SensitiveWord> {
    try {
      const response = await post(commentApi.SENSITIVE_WORDS, { word });
      return (response.data || response) as SensitiveWord;
    } catch (error) {
      console.error('创建敏感词失败:', error);
      throw error;
    }
  }

  /**
   * 批量创建敏感词
   */
  async createSensitiveWordsBatch(words: string[]): Promise<unknown> {
    try {
      const response = await post(commentApi.SENSITIVE_WORDS_BATCH, { words });
      return response.data || response;
    } catch (error) {
      console.error('批量创建敏感词失败:', error);
      throw error;
    }
  }

  /**
   * 更新敏感词
   */
  async updateSensitiveWord(id: number, word: string): Promise<SensitiveWord> {
    try {
      const response = await patch(commentApi.SENSITIVE_WORD_DETAIL(id), { word });
      return (response.data || response) as SensitiveWord;
    } catch (error) {
      console.error(`更新敏感词失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 更新敏感词状态
   */
  async updateSensitiveWordStatus(id: number, isActive: number): Promise<SensitiveWord> {
    try {
      const response = await patch(commentApi.SENSITIVE_WORD_STATUS(id), { isActive });
      return (response.data || response) as SensitiveWord;
    } catch (error) {
      console.error(`更新敏感词状态失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 删除敏感词
   */
  async deleteSensitiveWord(id: number): Promise<void> {
    try {
      await del(commentApi.SENSITIVE_WORD_DETAIL(id));
    } catch (error) {
      console.error(`删除敏感词失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 批量删除敏感词
   */
  async deleteSensitiveWordsBatch(ids: number[]): Promise<void> {
    try {
      await del(commentApi.SENSITIVE_WORDS_BATCH, { params: { ids } });
    } catch (error) {
      console.error('批量删除敏感词失败:', error);
      throw error;
    }
  }
}

export const sensitiveWordService = new SensitiveWordService();
